package sas

import (
	"encoding/json"
	"fmt"

	"gitee.com/micro-plat/sas/sas/modules/const/enum"
	"gitee.com/micro-plat/sas/sas/modules/const/models"
	"github.com/micro-plat/lib4go/errs"
)

//SecretGenerate 生成密钥
//sceretType:加密类型:md5,sha1,sha256,sha512,hmac,rsa,aes,des,
//expire :如果是设置rsa密钥需要才需要传入此参数, 设置为0,表示不过期
//ext: 扩展参数,长度必须为2的倍数,奇数为key,偶数为value,sceretType=rsa时可用参数,key1:pkcs_type,value1:PKCS1/PKCS8,不传默认为PKCS8  key2:bits,value2:1024/2048,不传默认为1024
func (n *sasClient) SecretGenerate(sceretType string, opts ...SecretOption) (resp *Response, err error) {
	input := &models.Item{}

	for i := range opts {
		opts[i](input)
	}

	if err = enum.CheckType(sceretType); err != nil {
		return
	}

	res, err := n.lib.Generate(n.euid, sceretType, input)
	if err != nil {
		return
	}

	resp, err = getResp(res)
	return
}

//SecretGet 获取密钥
//sceretType:加密类型:md5,sha1,sha256,sha512,hmac,rsa,aes,des,
func (n *sasClient) SecretGet(sceretType ...string) (resp *Response, err error) {
	if err = enum.CheckType(sceretType...); err != nil {
		return
	}

	res, err := n.lib.Get(n.euid, sceretType...)
	if err != nil {
		return
	}

	resp, err = getResp(res)
	return
}

//SecretReset 重置密钥
//sceretType:加密类型:md5,sha1,sha256,sha512,hmac,rsa,aes,des,
//ext: 扩展参数,长度必须为2的倍数,奇数为key,偶数为value,sceretType=rsa时可用参数,key1:pkcs_type,value1:PKCS1/PKCS8,不传默认为PKCS8  key2:bits,value2:1024/2048,不传默认为1024
func (n *sasClient) SecretReset(sceretType string, opts ...SecretOption) (resp *Response, err error) {
	input := &models.Item{}
	for i := range opts {
		opts[i](input)
	}

	if err = enum.CheckType(sceretType); err != nil {
		return
	}

	res, err := n.lib.Reset(n.euid, sceretType, input)
	if err != nil {
		return nil, err
	}

	resp, err = getResp(res)
	return
}

//SecretEnable 启用密钥
//sceretType:加密类型:md5,sha1,sha256,sha512,hmac,rsa,aes,des,
func (n *sasClient) SecretEnable(sceretType string) (resp *Response, err error) {

	if err = enum.CheckType(sceretType); err != nil {
		return
	}

	res, err := n.lib.Enable(n.euid, sceretType)
	if err != nil {
		return nil, err
	}

	resp, err = getResp(res)
	return
}

//SecretDisable 禁用密钥
//sceretType:加密类型:md5,sha1,sha256,sha512,hmac,rsa,aes,des,
func (n *sasClient) SecretDisable(sceretType string) (resp *Response, err error) {

	if err = enum.CheckType(sceretType); err != nil {
		return
	}

	res, err := n.lib.Disable(n.euid, sceretType)
	if err != nil {
		return nil, err
	}

	resp, err = getResp(res)
	return
}

//SecretSet 设置密钥
//sceretType:加密类型:md5,sha1,sha256,sha512,hmac,rsa,aes,des,
//secret :新密钥,sceretType!=rsa时必传
//publicKey :新rsa公钥,sceretType==rsa时必传
//privateKey :新rsa私钥,sceretType==rsa时必传
func (n *sasClient) SecretSet(sceretType string, opts ...SecretOption) (resp *Response, err error) {
	item := &models.Item{}
	for i := range opts {
		opts[i](item)
	}

	if err = enum.CheckType(sceretType); err != nil {
		return
	}

	res, err := n.lib.Set(n.euid, sceretType, item)
	if err != nil {
		return nil, err
	}

	resp, err = getResp(res)
	return
}

func getResp(res interface{}) (resp *Response, err error) {
	resp = &Response{}
	content, err := json.Marshal(res)
	if err != nil {
		return nil, err
	}

	if err := json.Unmarshal(content, resp); err != nil {
		return nil, errs.NewError(enum.ERR_PARAMS, fmt.Errorf("结果反序列化异常,err:%+v,content:%s", err, string(content)))
	}

	return
}
